/*
 * Created by JFormDesigner on Mon Dec 21 14:03:39 CST 2020
 */

package cn.fourhundredandeighteen.view.TeacherView.teacherCourseInfo;

import cn.fourhundredandeighteen.UserInfo;
import cn.fourhundredandeighteen.controller.CourseController;
import cn.fourhundredandeighteen.view.Component.Button;
import cn.fourhundredandeighteen.view.Component.Container;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Map;

/**
 * @author root
 */
public class updataCourse extends JPanel {
    private final ArrayList<JLabel> jLabels = new ArrayList<>();
    public int num;
    private ArrayList<JTextField> jTextFields = new ArrayList<>();

    private techercourse techercourse = new techercourse();
    private CourseController courseController = new CourseController();
    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JTextField textField1;
    private JLabel label2;
    private JTextField textField2;
    private JLabel label3;
    private JTextField textField3;
    private JLabel label4;
    private JTextField textField4;
    private JLabel label5;
    private JTextField textField5;
    private JLabel label6;
    private JTextField textField6;
    private JButton button1;
    private JLabel label9;
    public updataCourse() {
        this.setBackground(new Color(242, 242, 242));
        initComponents();
        getdata();
    }

    public void getdata() {
        Map<Integer, String[]> integerMap = techercourse.courseInfo();
        num = integerMap.get(1).length;//计算长度
        jLabels.add(label1);
        jLabels.add(label2);
        jLabels.add(label3);
        jLabels.add(label4);
        jLabels.add(label5);
        jLabels.add(label6);
        jTextFields.add(textField1);
        jTextFields.add(textField2);
        jTextFields.add(textField3);
        jTextFields.add(textField4);
        jTextFields.add(textField5);
        jTextFields.add(textField6);
        for (int i = 0; i < num; i++) {
            jLabels.get(i).setText(integerMap.get(1)[i]);
        }
        jTextFields.get(4).setText(String.valueOf(UserInfo.Session));
        jTextFields.get(4).setEditable(false);
    }

    private void button1ActionPerformed(ActionEvent e) {
        // TODO add your code here

        for (int i = 0; i < num; i++) {
            jTextFields.get(i).setText(jTextFields.get(i).getText());
        }
        String s = techercourse.updatacourseInfo(jTextFields);    //课程信息更新
        JOptionPane.showConfirmDialog(null, s, "提示", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
        Container.getInstance().upDatePage();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        textField1 = new JTextField();
        label2 = new JLabel();
        textField2 = new JTextField();
        label3 = new JLabel();
        textField3 = new JTextField();
        label4 = new JLabel();
        textField4 = new JTextField();
        label5 = new JLabel();
        textField5 = new JTextField();
        label6 = new JLabel();
        textField6 = new JTextField();
        button1 = new Button();
        label9 = new JLabel();

        //======== this ========
        setLayout(new GridBagLayout());
        ((GridBagLayout) getLayout()).columnWidths = new int[]{87, 69, 0, 0, 207, 0};
        ((GridBagLayout) getLayout()).rowHeights = new int[]{43, 55, 55, 51, 55, 55, 54, 24, 0};
        ((GridBagLayout) getLayout()).columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4};
        ((GridBagLayout) getLayout()).rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4};

        //---- label1 ----
        label1.setText("text");
        label1.setMaximumSize(new Dimension(23, 25));
        add(label1, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 25, 15), 0, 0));
        add(textField1, new GridBagConstraints(2, 1, 3, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 25, 0), 0, 0));

        //---- label2 ----
        label2.setText("text");
        add(label2, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 25, 15), 0, 0));
        add(textField2, new GridBagConstraints(2, 2, 3, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 25, 0), 0, 0));

        //---- label3 ----
        label3.setText("text");
        add(label3, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 25, 15), 0, 0));
        add(textField3, new GridBagConstraints(2, 3, 3, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 25, 0), 0, 0));

        //---- label4 ----
        label4.setText("text");
        add(label4, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 25, 15), 0, 0));
        add(textField4, new GridBagConstraints(2, 4, 3, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 25, 0), 0, 0));

        //---- label5 ----
        label5.setText("text");
        add(label5, new GridBagConstraints(1, 5, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 25, 15), 0, 0));
        add(textField5, new GridBagConstraints(2, 5, 3, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 25, 0), 0, 0));

        //---- label6 ----
        label6.setText("text");
        add(label6, new GridBagConstraints(1, 6, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 25, 15), 0, 0));
        add(textField6, new GridBagConstraints(2, 6, 3, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 25, 0), 0, 0));

        //---- button1 ----
        button1.setText("\u4fee\u6539");
        button1.addActionListener(e -> button1ActionPerformed(e));
        add(button1, new GridBagConstraints(2, 7, 2, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 0, 15), 0, 0));
        add(label9, new GridBagConstraints(4, 7, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 0, 0), 0, 0));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
